package com.rpframework.module.common.springmvc.bind;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期注册类
 */
public class DateEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = format.parse(text);
        } catch (ParseException e) {
            format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            try {
                date = format.parse(text);
            } catch (ParseException e4) {
                format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date = format.parse(text);
                } catch (ParseException e1) {
                    format = new SimpleDateFormat("hh:mm:ss");
                    try {
                        date = format.parse(text);
                    } catch (ParseException e2) {
                        throw new RuntimeException(e2);
                    }
                }
            }

        }
        setValue(date);
    }
}